/**
 * 一个在JavaFX中导出Pane中Shape的示例
 *
 * @Author: xiaolei
 */
package xdrawer.test.shape;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class ShapeExportTester extends Application {
    private BorderPane root;
    private Pane pane;    // 绘图pane
    private HBox toolbox; // 工具条

    @Override
    public void start(Stage primaryStage) throws Exception {
        pane = new Pane();
        toolbox = new HBox();
        Button bShape = new Button("产生一个形状");
        bShape.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Shape shape = null;
                int type = (int) (Math.random() * 3);
                Color c = new Color(Math.random(), Math.random(), Math.random(), 1.0);
                switch (type) {
                    case 0:
                        shape = new Circle(Math.random() * 500, Math.random() * 400, 20);
                        break;
                    case 1:
                        shape = new Rectangle(Math.random() * 500, Math.random() * 400, 30, 20);
                        break;
                    case 2:
                        shape = new Line(Math.random() * 500, Math.random() * 400, Math.random() * 500, Math.random() * 400);
                }
                shape.setFill(c);
                pane.getChildren().add(shape);
            }
        });
        Button bExportAll = new Button("导出图片(pane)");
        bExportAll.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                SnapshotParameters parameters = new SnapshotParameters();
                WritableImage img = pane.snapshot(null, null);
                File file = new File("export_ALL_" + System.currentTimeMillis() + ".png");
                try {
                    ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

        Button bExportFit = new Button("导出图片(Fit)");
        bExportFit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                SnapshotParameters parameters = new SnapshotParameters();
                double minX = pane.getWidth();
                double minY = pane.getHeight();
                double maxX = 0;
                double maxY = 0;
                for (Node node : pane.getChildren()) {
                    Bounds bounds = node.getLayoutBounds();
                    minX = minX < bounds.getMinX() ? minX : bounds.getMinX();
                    minY = minY < bounds.getMinY() ? minY : bounds.getMinY();
                    maxX = maxX > bounds.getMaxX() ? maxX : bounds.getMaxX();
                    maxY = maxY > bounds.getMaxY() ? maxY : bounds.getMaxY();
                }
                System.out.println(minX + "," + minY);
                System.out.println(maxX + "," + maxY);
                parameters.setViewport(new Rectangle2D(minX, minY, maxX - minX, maxY - minY));
                WritableImage img = pane.snapshot(parameters, null);
                File file = new File("export_FIT_" + System.currentTimeMillis() + ".png");
                try {
                    ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
        toolbox.getChildren().addAll(bShape, bExportAll, bExportFit);

        root = new BorderPane();
        root.setCenter(pane);
        root.setTop(toolbox);

        primaryStage.setTitle("XDrawer");
        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.show();


    }

    public static void main(String[] args) {
        Application.launch(args);
    }


}
